<?php

namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller {

    public function index() {

        $this->redirect("login");
    }


    //后台登录首页
    public function login() {


        //如果已经登录直接跳转
        if (isset($_SESSION['user']['id']) && !empty($_SESSION['user']['id'])) {

            $this->redirect("User/index");
            exit;
        }


        if (IS_POST) {
            $map = I("post.");  
            $verify_code = checkVerify($map['code']);
            if (!$verify_code) {
                $checkInfo = array('code' => 100, 'mess' => "验证码错误");
                echo json_encode($checkInfo);
                exit;
            }
            $userData = array(
                'username' => $map['username'],
                'password' => md5($map['password'])
            );
            $userInfo = M('user')->where($userData)->find();
            if (!$userInfo) {
                $checkInfo = array('code' => 100, 'mess' => "账号或密码错误");
                echo json_encode($checkInfo);
                exit;
            }

            $ip = getUserIp();

            $data = array(
                'id' => $userInfo['id'],
                'last_ip' => $ip
            );
            M('user')->save($data);

            session('user', $userInfo);
            $checkInfo = array('code' => 200, 'mess' => "登录成功");
            echo json_encode($checkInfo);
            exit;
        } else {
            $this->display();
        }
    }

    //登出会员
    public function loginOut() {

        $_SESSION = ""; //清除SESSION值.
        if (isset($_COOKIE[session_name()])) {  //判断客户端的cookie文件是否存在,存在的话将其设置为过期.
            setcookie(session_name(), '', time() - 1, '/');
        }

        session_destroy();  //清除服务器的sesion文件
        $this->redirect("/Home/Login/login");
    }

    //生成验证码
    Public function yanzheng() {
//             import('ORG.Util.Image');
        ob_end_clean();
//             Image::buildImageVerify();
//         }
        $config = array(
            'fontSize' => 50, // 验证码字体大小
            'length' => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'useCurve' => false,
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();

//            验证码生成的数组保存在session   MD5加密
//            Array ( [d2d977c58444271d9c780187e93f80e5] => Array ( [verify_code] => f634109581998a1ec737219a10b0563f [verify_time] => 1453779056 ) )

        }
    public function register(){
        if(IS_POST){

            $map=I('post.');

            $where=array(
                'username'=>$map['username'],
                'phone'=>$map['tel'],
                'email'=>$map['email'],
                '_logic'=>'or'
                );
            $user=M('user')->where($where)->find();
            if($user){
                exit("2");
            }


            if($where[''])


           $data=array(
                'username'=>$map['username'],
                'password'=>md5($map['password']),
                'email'=>$map['email'],
                'phone'=>$map['tel'],
                'sex'=>$map['sex'],
                'last_ip'=>getUserIp(),
                'reg_time'=>date("Y-m-d H:i:s")
            );

           $res=M('user')->add($data);

           if(!$res){
            exit("0");
           }else{
            $userInfo=M('user')->where('id='.$res)->find();
            if($userInfo){
                session('user', $userInfo);
            }
            exit("1");
           }

        }else{
           $this->display(); 
        }
        
    }
 




}

